package springboot.init;

import org.opentcs.data.order.TransportOrder;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import springboot.bean.Taskzc;
import springboot.bean.TaskzcExample;
import springboot.dao.TaskzcDAO;
import springboot.tool.KernelTools;

import javax.annotation.Resource;
import java.util.List;

@Component
@Order(value = 10)
public class CompareKernelWithMysql implements CommandLineRunner {


    private KernelTools km=new KernelTools();
    @Resource
    private TaskzcDAO taskzcDAO;//1~7

    private TaskzcExample taskzcExample=null;
    private TaskzcExample.Criteria criteria=null;
    private List<Taskzc> list=null;
    private TransportOrder transportOrder=null;

    @Override
    public void run(String... args) throws Exception {
        //关闭调度软件再重新打开时,调用此方法,比对一下,完成就让该任务的状态处于完成状态，如果没完成，就让状态继续更新
        taskzcExample=new TaskzcExample();
        criteria=taskzcExample.createCriteria();
        criteria.andStatusBetween(1,7);
        list=taskzcDAO.selectByExample(taskzcExample);
        if (list.size()>0){
            for (Taskzc taskzc:list) {
                transportOrder=km.getTO(taskzc.getName());
                if (transportOrder!=null){
                    String taskStatus = km.getOrderStatus(taskzc.getName());
                    if (taskStatus.equals("FINISHED")){
                        if (taskzc.getCategory()==0){
                            taskzc.setStatus(80);
                        }else {
                            taskzc.setStatus(100);
                        }
                        taskzcDAO.updateByPrimaryKey(taskzc);
                    }else if (taskStatus.equals("FAILED")){
                        if (taskzc.getCategory()==0){
                            taskzc.setStatus(98);
                        }else {
                            taskzc.setStatus(99);
                        }
                        taskzcDAO.updateByPrimaryKey(taskzc);
                    }
                }
            }
        }
    }
}
